package ru.spb.osll.myfit.server.execute;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.easymock.EasyMock;
import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import ru.spb.osll.myfit.server.Configurator;
import ru.spb.osll.myfit.util.FileHelper;

public class PageExecutorFactoryTest {
	private static final String CONFIG_FILE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
						  "  <doc>\n" +
						  "     <jars name=\"JARS\" path=\"jars\"></jars>\n" +	
						  "     <fixtures name=\"FIXTURES\" path=\"fixtures\"></fixtures>\n" + 
						  "     <root name=\"ROOT\" path=\"root\"></root>\n" +
						  "     <execresults name=\"EXECRESULTS\" path=\"results\"></execresults>\n" +
						  "     <resources name=\"RESOURCES\" path=\"resources\"></resources>\n" +
						  "  </doc>\n";
	private static final String LOG = "logFile=%s\n" + "loggerName=%s\n";

	private final String PAGE_PATH = "TestPage";
	private final Date now = new Date();
	private final String UNAME = FileHelper.SEPARATOR + (new SimpleDateFormat(FileHelper.DATE_FORMAT)).format(now);

	private static final Configurator configurator = getConfigurator();

	private static Configurator getConfigurator() {
		try {
			DocumentBuilder xml = null;
			xml = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			InputStream is = new ByteArrayInputStream(CONFIG_FILE.getBytes());
			Document doc = xml.parse(is);
			Configurator configurator = Configurator.getInstance();
			configurator.loadConfigFile(doc);
			return configurator;
		} catch (ParserConfigurationException ex) {
		} catch (SAXException ex) {
		} catch (IOException ex) {
		}

		return null;
	}

	@Test
	public void testCreatePageExecutor() throws Exception {
		String root = configurator.getRoot();
		String fixtures = configurator.getFixtures();
		String jars = configurator.getJars();
		String results = configurator.getTestResultsDirectory();

		String base = results + File.separator + PAGE_PATH + UNAME;
		String logdir = base + File.separator + "loggerconfig";
		String logfile = logdir + File.separator + "logconfig.properties";

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		FileHelper helper = EasyMock.createStrictMock(FileHelper.class);

		EasyMock.expect(helper.createUDir(results, PAGE_PATH)).andReturn(base);
		EasyMock.expect(helper.parseUNameGetTime(PAGE_PATH + UNAME)).andReturn(now);
		helper.copyDir(root, base + File.separator + "root");
		helper.copyDir(fixtures, base + File.separator + "fixtures");
		helper.copyDir(jars, base + File.separator + "jars");
		EasyMock.expect(helper.createDir(logdir)).andReturn(true);
		EasyMock.expect(helper.openFile(logfile)).andReturn(baos);
		EasyMock.replay(helper);

		PageExecutorFactory factory = new PageExecutorFactory(configurator, helper);
		PageExecutor executor = factory.createPageExecutor(PAGE_PATH);

		assertNotNull(executor);
		assertEquals(String.format(LOG, results + "/" + PAGE_PATH + UNAME + "/" + PAGE_PATH + ".log"
						, PAGE_PATH + UNAME), baos.toString().replaceAll("\r", ""));

		EasyMock.verify(helper);
	}
}

